package microcredit.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.border.EtchedBorder;
import javax.swing.border.MatteBorder;

import microcredit.gui.util.ExceptionDialog;
import net.miginfocom.swing.MigLayout;

public class AboutDialog extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel mainPanel;
	private JButton closeButton;
	private JScrollPane scrollPane;
	private JTextPane aboutTextfield;
	private JButton demoErrorButton;

	/**
	 * Create the dialog.
	 */
	public AboutDialog(JFrame parent) {
		super(parent, true);
		initialize();
		setLocationRelativeTo(getParent());
		setUndecorated(true);
		
		getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "dispose");
		getRootPane().getActionMap().put("dispose", new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		getRootPane().setDefaultButton(getCloseButton());
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				getCloseButton().requestFocus();
			}
		});
	}
	private void initialize() {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle("About Microcredit");
		setBounds(100, 100, 300, 380);
		getContentPane().add(getMainPanel(), BorderLayout.CENTER);
		getMainPanel().setLayout(new MigLayout("", "0[295px,right]0", "0[347px]0[23px]0"));
		getMainPanel().add(getScrollPane(), "cell 0 0,grow");
		getMainPanel().add(getDemoErrorButton(), "flowx,cell 0 1");
		getMainPanel().add(getCloseButton(), "cell 0 1,alignx right,aligny center");
		getScrollPane().setViewportView(getAboutTextfield());
	}

	private JPanel getMainPanel() {
		if (mainPanel == null) {
			mainPanel = new JPanel();
			mainPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			mainPanel.setName("mainPanel");
			mainPanel.setName("panel");
		}
		return mainPanel;
	}
	private JButton getCloseButton() {
		if (closeButton == null) {
			closeButton = new JButton("Close");
			closeButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					closeButtonActionPerformed(e);
				}
			});
			closeButton.setName("closeButton");
			closeButton.setName("button");
		}
		return closeButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBorder(new MatteBorder(0, 0, 1, 0, (Color) Color.GRAY));
			scrollPane.setName("scrollPane");
		}
		return scrollPane;
	}
	private JTextPane getAboutTextfield() {
		if (aboutTextfield == null) {
			aboutTextfield = new JTextPane() {
				private static final long serialVersionUID = 1L;
				@Override
				protected void paintComponent(Graphics g) {
					super.paintComponent(g);
					ImageIcon icon = new ImageIcon(getClass().getResource("/microcredit/icons/x48/environment_add.png"));
					
					g.drawImage(icon.getImage(), 5, 10, icon.getImageObserver());
					
					icon = new ImageIcon(getClass().getResource("/microcredit/icons/daraja.png"));
					g.drawImage(icon.getImage(), 5, 100, icon.getImageObserver());
				}
			};
			aboutTextfield.setRequestFocusEnabled(false);
			aboutTextfield.setOpaque(false);
			aboutTextfield.setEditable(false);
			aboutTextfield.setContentType("text/html");
			StringBuffer b = new StringBuffer();
			b.append("<html><head/><body><br>");
			b.append("<h1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Microcredit v "+microcredit.Version.VERSION+"</h1>");
			
			b.append("<br>Hosted by Daraja non-profit organization:<br>");
			
			b.append("<br><br><br><br><br><br><br>Gracefully sponsored and developed by: <br>");
			b.append("<br><b>Christian Humer </b>(christian.humer@gmail.com)");
			b.append("<br><b>Florian Humer </b>(florian.humer@gmail.com)");
			b.append("<font size=-1><br><br>Microcredit  Copyright (C) 2010  Christian Humer");
            b.append("<br>This program comes with ABSOLUTELY NO WARRANTY.");
            b.append("<br>This is free software, and you are welcome to redistribute it.</font>");

			b.append("</body></html>");
			aboutTextfield.setText(b.toString());
			
			aboutTextfield.setName("aboutTextfield");
			aboutTextfield.setBackground(Color.LIGHT_GRAY);
			aboutTextfield.setName("textArea");
		}
		return aboutTextfield;
	}
	protected void closeButtonActionPerformed(ActionEvent e) {
		dispose();
	}
	private JButton getDemoErrorButton() {
		if (demoErrorButton == null) {
			demoErrorButton = new JButton("Show error");
			demoErrorButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ExceptionDialog.show(AboutDialog.this, new Exception("Demonstration exception"));
				}
			});
		}
		return demoErrorButton;
	}
}
